package com.lambda.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by micr on 2018/3/6 0006.
 * 字体大小自适应
 */
public class FitTextView extends View {
    private Paint mTextPaint;
    private String mText;
    private int mTextColor;
    private int mMaxTextSize;

    public FitTextView(Context context) {
        this(context, null);
    }

    public FitTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (attrs != null) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FitTextView);
            mText = array.getString(R.styleable.FitTextView_ftv_text);
            mTextColor = array.getColor(R.styleable.FitTextView_ftv_text_color, Color.WHITE);
            mMaxTextSize = array.getDimensionPixelSize(R.styleable.FitTextView_ftv_text_size, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
            array.recycle();
        }

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(mMaxTextSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode != MeasureSpec.EXACTLY) {
            final int textWidth = (int) mTextPaint.measureText(mText);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(textWidth, MeasureSpec.EXACTLY);
        }

        if (heightMode != MeasureSpec.EXACTLY) {
            Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(fontMetricsInt.bottom - fontMetricsInt.top, MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setText(String text) {
        if (!TextUtils.isEmpty(text) && !TextUtils.equals(mText, text)) {
            mText = text;
            postInvalidate();
        }
    }

    public void setmTextColor(int color) {
        mTextPaint.setColor(color);
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        final float validWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        float textWidth = mTextPaint.measureText(mText);
        if (textWidth > validWidth) {
            float ts = -1;
            do {
                if (ts == -1) {
                    ts = mMaxTextSize * validWidth / textWidth;
                } else {
                    ts = ts * validWidth / textWidth;
                }
                mTextPaint.setTextSize(ts);
                textWidth = mTextPaint.measureText(mText);
            } while (textWidth > validWidth);
        } else {
            mTextPaint.setTextSize(mMaxTextSize);
        }
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        final float fontHeight = fontMetrics.leading - fontMetrics.ascent;
        canvas.drawText(mText, getPaddingLeft() + validWidth / 2, (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 + (fontMetrics.ascent - fontMetrics.top) / 2 + fontHeight, mTextPaint);
    }
}
